package com.ctcc.serviceImpl;

import com.ctcc.entity.count.*;
import com.ctcc.repository.count.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Peanut on 2018/2/26.
 */
@Service
public class CountImpl {
    @Autowired
    MemoryRepository memoryRepository;
    @Autowired
    DbZhiDuRepository dbZhiDuRepository;

    @Autowired
    DbZhuBeiRepository dbZhuBeiRepository;
    @Autowired
    MemRepository memRepository;



    public  List<Memory> getMemoryKind(Long cid) {
        List<Memory> memoryList=new ArrayList<>();
        if (StringUtils.isNotEmpty(toString().valueOf(cid))) {
          memoryList = memoryRepository.findAll(new Specification<Memory>() {
                @Override
                public Predicate toPredicate(Root<Memory> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

                    return criteriaBuilder.and(criteriaBuilder.equal(root.get(Memory_.cd), cid));
                }
            });
            if(memoryList.size()>0){
                return memoryList;
            }
        }
          return memoryList;
    }

    /***
     * 得到所有云主机存储的信息
     * @return
     */
    public List<MemShop> getAllMemShop(){
            return memRepository.findAll();
    }





    public  DbZhiDuMemory getDbZdPrice(Long cpuVal,String memVal){
        return dbZhiDuRepository.findOne(new Specification<DbZhiDuMemory>() {
            @Override
            public Predicate toPredicate(Root<DbZhiDuMemory> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

                return criteriaBuilder.and(criteriaBuilder.equal(root.get(DbZhiDuMemory_.cd), cpuVal),
                        criteriaBuilder.equal(root.get(DbZhiDuMemory_.memory), memVal));
            }
        });

    }



    public  DbZhuBeiMemory getDbZbPrice(Long cpuVal,String memVal){
        return dbZhuBeiRepository.findOne(new Specification<DbZhuBeiMemory>() {
            @Override
            public Predicate toPredicate(Root<DbZhuBeiMemory> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

                return criteriaBuilder.and(criteriaBuilder.equal(root.get(DbZhuBeiMemory_.cd), cpuVal),
                        criteriaBuilder.equal(root.get(DbZhuBeiMemory_.memory), memVal));
            }
        });

    }


    public  Memory getPrice(Long cpuVal,String memVal){
        return memoryRepository.findOne(new Specification<Memory>() {
            @Override
            public Predicate toPredicate(Root<Memory> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

                return criteriaBuilder.and(criteriaBuilder.equal(root.get(Memory_.cd), cpuVal),
                        criteriaBuilder.equal(root.get(Memory_.memory), memVal));
            }
        });

    }

    public List<DbZhuBeiMemory> getDbMemoryKind(Long cid){

        return dbZhuBeiRepository.findAll(new Specification<DbZhuBeiMemory>() {
            @Override
            public Predicate toPredicate(Root<DbZhuBeiMemory> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

                return criteriaBuilder.and(criteriaBuilder.equal(root.get(DbZhuBeiMemory_.cd), cid));
            }
        });

    }


    public List<DbZhiDuMemory> getDbZhiDuMemoryKind(Long cid){

        return dbZhiDuRepository.findAll(new Specification<DbZhiDuMemory>() {
            @Override
            public Predicate toPredicate(Root<DbZhiDuMemory> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

                return criteriaBuilder.and(criteriaBuilder.equal(root.get(DbZhiDuMemory_.cd), cid));
            }
        });

    }


    /***
     * 后台管理模块方法
     * 云主机
     */
    public  List<Memory> getAllYunZhuJiInfo() {
        return memoryRepository.findAll();
    }
    public  Memory getYunZhuJiInfo(Long id) {
        return memoryRepository.findOne(id);
    }

    public  DbZhuBeiMemory getGuanXiXingZbInfo(Long id) {
        return dbZhuBeiRepository.findOne(id);
    }
    public  DbZhiDuMemory getGuanXiXingZdInfo(Long id) {
        return dbZhiDuRepository.findOne(id);
    }
    public  void  saveYunZhuJi(Memory memory) {
         memoryRepository.save(memory);
    }

    public  void  saveGuanXiXingZb(DbZhuBeiMemory dbZhuBeiMemory) {
        dbZhuBeiRepository.save(dbZhuBeiMemory);
    }
    public  void  saveGuanXiXingZd(DbZhiDuMemory dbZhiDuMemory) {
        dbZhiDuRepository.save(dbZhiDuMemory);
    }

    public List<DbZhuBeiMemory> getAllGuanxixingOfZb(){
        return dbZhuBeiRepository.findAll();
    }

    public List<DbZhiDuMemory> getAllGuanxixingOfZd(){
        return dbZhiDuRepository.findAll();
    }
}
